package com.example.zhiran.service.userservice;

import com.example.zhiran.exception.OptionResultInitialException;
import com.example.zhiran.mapper.ZRUserMapper;
import com.example.zhiran.pojo.front.SignupUser;
import com.example.zhiran.pojo.option.OptionResult;
import com.example.zhiran.pojo.option.useroption.UserOption;
import com.example.zhiran.pojo.option.useroption.UserOptionMessage;
import com.example.zhiran.pojo.orm.ZRUser;
import com.example.zhiran.service.OptionResultBuilder;
import com.example.zhiran.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.Date;

@Service
public class ZRUserService {
    @Autowired
    private ZRUserMapper zrUserMapper;

    public OptionResult userLogin(String account, String password)  {
        ZRUser zrUser;
        if (StringUtil.isTel(account)){
            System.out.println("以电话号码方式登录");
            zrUser = getUserByTel(account);
        }else {
            System.out.println("以昵称形式登录");
            zrUser = getUserByName(account);
        }
        OptionResultBuilder optionResultBuilder = new OptionResultBuilder()
                .setOptionCode(UserOption.USER_LOIN.getOptionCode());
        if (zrUser==null){      //不存在
            return  optionResultBuilder
                    .setStatus(OptionResult.FAILURE)
                    .setMessageCode(UserOptionMessage.LOGIN_NON_EXISTENT_MSG.getMessageCode())
                    .build();
        }else if (zrUser.getZrPassword().equals(password)){     //登录成功
            return optionResultBuilder
                    .setStatus(OptionResult.SUCCESS)
                    .setResult(zrUser)
                    .setMessageCode(UserOptionMessage.LOGIN_SUCCESS_MSG.getMessageCode())
                    .build();
        }else {     //密码错误
            return optionResultBuilder
                    .setStatus(OptionResult.FAILURE)
                    .setMessageCode(UserOptionMessage.LOGIN_PASSWORD_INCORRECT_MSG.getMessageCode())
                    .build();
        }

    }
    public ZRUser getUserById(String id){
        return zrUserMapper.getUserById(id);
    }

    public ZRUser getUserByName(String name){
        return zrUserMapper.getUserByName(name);
    }

     public ZRUser getUserByTel(String tel){
        return zrUserMapper.getUserByTel(tel);
    }

    @Transactional
    public OptionResult addUser(SignupUser user) throws OptionResultInitialException {
        boolean isNameOccupied = isNickNameOccupied(user.getNickname());
        boolean isTelRegistered = isTelRegistered(user.getTel());

        OptionResultBuilder optionResultBuilder = new OptionResultBuilder()
                .setOptionCode(UserOption.USER_LOIN.getOptionCode());
        if (!isTelRegistered && !isNameOccupied){       //没有昵称和电话冲突
            int count = zrUserMapper.getUserCount();
            String zrId ="zr"+ new DecimalFormat("0000000").format(count+1);
            ZRUser zrUser = new ZRUser(zrId, user.getNickname(), user.getTel(), user.getPwd(), ZRUser.DEFAULT_LEVEL,ZRUser.DEFAULT_GROWTH, ZRUser.DEFAULT_BEANS,new Date());
            //dev multiThread
            new Thread(()->{
                zrUserMapper.addUser(zrUser);
            });
            return optionResultBuilder
                    .setStatus(OptionResult.SUCCESS)
                    .setMessageCode(UserOptionMessage.LOGIN_SUCCESS_MSG.getMessageCode())
                    .build();

        }else if(isNameOccupied) {      //用户名冲突
            return optionResultBuilder.
                    setStatus(OptionResult.FAILURE)
                    .setMessageCode(UserOptionMessage.NICKNAME_OCCUPIED_MSG.getMessageCode())
                    .build();
        }else {         //电话已被注册
            return optionResultBuilder
                    .setStatus(OptionResult.FAILURE)
                    .setMessageCode(UserOptionMessage.SIGNUP_TELEPHONE_OCCUPIED_MSG.getMessageCode())
                    .build();
        }
    }

    public OptionResult updateUser(ZRUser zrUser){
        OptionResultBuilder optionResultBuilder = new OptionResultBuilder()
                .setOptionCode(UserOption.USER_UPDATE.getOptionCode());
        //todo  没有占用
        if (!isNickNameOccupied(zrUser.getZrNickname())){
            zrUserMapper.updateUserNickname(zrUser.getZrId(), zrUser.getZrNickname());
            return optionResultBuilder
                    .setStatus(OptionResult.SUCCESS)
                    .setMessageCode(UserOptionMessage.UPDATE_SUCCESS_MSG.getMessageCode())
                    .build();
        } else {
            return optionResultBuilder
                    .setStatus(OptionResult.FAILURE)
                    .setMessageCode(UserOptionMessage.NICKNAME_OCCUPIED_MSG.getMessageCode())
                    .build();
        }
    }

    public OptionResult updateUserNickname(String zrId, String zrNickname){
        OptionResultBuilder optionResultBuilder = new OptionResultBuilder()
                .setOptionCode(UserOption.USER_UPDATE.getOptionCode());
        //昵称没有被占用
        if (!isNickNameOccupied(zrNickname)){
            zrUserMapper.updateUserNickname(zrId, zrNickname);
            return optionResultBuilder
                    .setStatus(OptionResult.SUCCESS)
                    .setMessageCode(UserOptionMessage.UPDATE_SUCCESS_MSG.getMessageCode())
                    .build();
        } else {
            return optionResultBuilder
                    .setStatus(OptionResult.FAILURE)
                    .setMessageCode(UserOptionMessage.NICKNAME_OCCUPIED_MSG.getMessageCode())
                    .build();
        }
    }

    //昵称是否被占用 占用=>true
    public boolean isNickNameOccupied(String name){
        ZRUser zrUser = getUserByName(name);
        return !(zrUser == null);
    }

    //电话是否被注册 注册=>true
    public boolean isTelRegistered(String tel){
        ZRUser zrUser = getUserByTel(tel);
        return !(zrUser==null);
    }
    public int deleteUser(String id){
        return zrUserMapper.deleteUser(id);
    }

}
